package com.android.powerbankpad.posutils;

import android.content.Context;

import com.android.powerbankpad.api.ApiUrl;
import com.android.powerbankpad.config.Constants;
import com.android.powerbankpad.network.CommonalityModel;
import com.android.powerbankpad.network.NetWorkListener;
import com.android.powerbankpad.network.okHttpModel;
import com.nlscan.android.usbserialdemo.UsbService;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ZhiTuHuiUtils {
    //智图汇扫码器
    private UsbService Tsexample;
    private PosListener posListener;

    private boolean isInit = false;

    public void setPosListener(PosListener posListener) {
        this.posListener = posListener;
    }

    public void initPos(Context context) {


         /* Tsexample = new Scan(this, myHandler);

        Scan.TsDataCallBack tsdatacallback = new Scan.TsDataCallBack() {
            @Override
            public int ts_get_data_fun(int iNode, byte[] pParam, byte[] pbuf, int uiBufLen) {
                byte[] decode = new byte[uiBufLen];   //数据域
                System.arraycopy(pbuf, 0, decode, 0, uiBufLen);

                LogcatUtils.e("wangshuo Scan.HANDLER_SCAN_DEC " + new String(decode));
                return 0;
            }
        };

        Scan.TsStateCallBack tsstatecallback = new Scan.TsStateCallBack() {
            @Override
            public int ts_scan_state_fun(int iNode, byte[] pParam, byte ucState) {
                String stbuf;
                byte[] ucBufTemp;
                byte[] ucBufTemp1;
                int iRet;
                LogcatUtils.e("wangshuo tsstatecallback state=" + ucState);

                if (ucState == 1) {

                    LogcatUtils.e("wangshuo tsstatecallback 设备连接成功 ");
                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_name(iNode, ucBufTemp, ucBufTemp.length);

                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_product_name iRet " + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_product_name iRet " + iRet);
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_id(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_product_id iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_product_id iRet " + iRet);
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_date(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_product_date iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_product_date iRet " + iRet );
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_name(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_custom_name iRet " + iRet + " stbuf " + stbuf);

                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_custom_name iRet " + iRet );
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_id(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_custom_id iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_custom_id iRet " + iRet );
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_date(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_custom_date iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_custom_date iRet " + iRet );
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_firmware_version(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_firmware_version iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_firmware_version iRet " + iRet);
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_hardware_version(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_hardware_version iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_hardware_version iRet " + iRet);
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_complex_sn(iNode, ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo ts_scan_get_complex_sn iRet " + iRet + " stbuf " + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo ts_scan_get_complex_sn iRet " + iRet );
                    }
                } else {
                    LogcatUtils.e("Scan 设备断开连接");
                }
                return 0;
            }
        };

        Tsexample.ts_scan_get_data_fun_register(null, tsdatacallback);
        Tsexample.ts_scan_state_fun_register(null, tsstatecallback);



        Tsexample.ts_scan_init();*/


        Tsexample = new UsbService(context);
        UsbService.TsDataCallBack tsdatacallback = new UsbService.TsDataCallBack() {
            @Override
            public int ts_get_data_fun(byte[] pParam, byte[] pbuf, int uiBufLen) {
                byte[] decode = new byte[uiBufLen];   //数据域
                System.arraycopy(pbuf, 0, decode, 0, uiBufLen);
                //ScanMsg(HANDLER_SCAN_DEC, 0, 0, new String(decode));
                /*ScanMsg(HANDLER_SCAN_DEC, 0, 0, new String(decode));

                System.out.printf("[%03d]len:%d, buf:",++uiDecodeCnt, uiBufLen);
                for (byte b : pbuf) {
                    System.out.printf("%c", b);
                }
                System.out.println();*/
                if (posListener != null) {
                    posListener.readCard();
                }
                leaseTerminal(new String(decode));
                LogcatUtils.e("wangshuo 交易号 " + new String(decode));
                return 0;
            }
        };

        UsbService.TsStateCallBack tsstatecallback = new UsbService.TsStateCallBack() {
            @Override
            public int ts_scan_state_fun(byte[] pParam, byte ucState) {
                String stbuf;
                byte[] ucBufTemp;
                byte[] ucBufTemp1;
                int iRet;
                LogcatUtils.e("wangshuo Scan state=" + ucState);

                if (ucState == 1) {
                    if (!isInit) {
                        if (posListener != null) {
                            posListener.connectDevice();
                        }
                        isInit = true;
                    } else {
                        if (posListener != null) {
                            posListener.reConnectDevice();
                        }
                    }

                    LogcatUtils.e("wangshuo 设备连接成功");
                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_name(ucBufTemp, ucBufTemp.length);

                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo product name:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo product name " + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_id(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo product id:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo product id:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_product_date(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo product date:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo product date:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_name(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo custom name:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo custom name:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_id(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo custom id:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo custom id:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_custom_date(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo custom date:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo custom date:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_firmware_version(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo firmware version:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo firmware version:" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_hardware_version(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo hardware version:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo hardware version:%s" + iRet + "null");
                    }

                    ucBufTemp = new byte[128];
                    iRet = Tsexample.ts_scan_get_complex_sn(ucBufTemp, ucBufTemp.length);
                    if (iRet > 0) {
                        ucBufTemp1 = new byte[iRet];
                        System.arraycopy(ucBufTemp, 0, ucBufTemp1, 0, iRet);
                        stbuf = new String(ucBufTemp1);
                        LogcatUtils.e("wangshuo COMPLEX SN:" + iRet + stbuf);
                    } else {
                        LogcatUtils.e("wangshuo COMPLEX SN:" + iRet + "null");
                    }
                } else {
                    if (posListener != null) {
                        posListener.disconnect();
                    }
                    LogcatUtils.e("wangshuo 设备断开连接");
                }
                return 0;
            }
        };
        Tsexample.ts_scan_get_data_fun_register(null, tsdatacallback);
        Tsexample.ts_scan_state_fun_register(null, tsstatecallback);
        LogcatUtils.e("wangshuo scan dll ver:" + Tsexample.ts_scan_get_version() + Tsexample.ts_scan_get_product_type());
        Tsexample.ts_scan_init();
    }

    private void leaseTerminal(String auth_code) {

        String baseUrl = CacheUtils.getString(Constants.base_url) + "/app/Paymentcode/payment";
        Map<String, String> params = new HashMap<>();
        params.put("sn", CacheUtils.getString(Constants.serial_number));
        params.put("ocode", CacheUtils.getString(Constants.ocode));
        params.put("auth_code", auth_code);

        LogcatUtils.e("wangshuo leaseTerminal baseUrl " + baseUrl);
        okHttpModel.post(baseUrl, params, ApiUrl.lease_Id, new NetWorkListener() {
            @Override
            public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) throws JSONException {
                if (posListener != null) {
                    posListener.deviceScanSucceed();
                }
                //ToastUtils.showShort(commonality.getMsg(), 5000);
            }

            @Override
            public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
                if (posListener != null) {
                    posListener.deviceScanSucceed();
                }
            }

            @Override
            public void onError(int id, Exception e) {
                if (posListener != null) {
                    posListener.deviceScanSucceed();
                }
            }
        });


    }

    public interface PosListener {


        void connectDevice();
        //重新连接
        void reConnectDevice();

        //断开连接
        void disconnect();

        //刷卡
        void readCard();

        //加载Ui
        void deviceScanSucceed();


    }
}
